Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | ACADEMIC RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * PROJECT METADATA8 * -----------------------------------------------------------------------------9 * Project : UQO Éval10 * File : src/app/api/metre/projects/[id]/plans/route.ts11 * Module : app12 * Repository : https://git.spboucher.ai/uqo-eval13 * Project Website : https://www.uqo-eval.app14 * Created : 2026-10-2815 * Last Modified : 2026-10-2816 *17 * =============================================================================18 * SOFTWARE MAINTAINER & AFFILIATION19 * =============================================================================20 *21 * Maintainer : Simon-Pierre Boucher22 * Position : Professor of Finance23 * Institution : Université du Québec en Outaouais (UQO)24 * Department : Département des sciences administratives25 * Website : https://spboucher.ai26 * Git : https://git.spboucher.ai27 *28 * Research Contact : research@spboucher.ai29 * Direct Contact : simon@spboucher.ai30 * Security/Admin : admin@spboucher.ai31 *32 * The affiliation is descriptive. It is not an institutional endorsement,33 * a license grant or a determination of ownership.34 *35 * =============================================================================36 * COPYRIGHT, AUTHORSHIP & DATA37 * =============================================================================38 *39 * Copyright (c) the author(s) of this work.40 * Ownership is not asserted by this notice; see the repository records.41 *42 * This header identifies the maintainer of the software. Scientific43 * authorship of any associated paper is stated in the paper itself and44 * is not changed by this notice. Datasets and third-party materials45 * remain the property of their respective owners and licensors.46 *47 * This file is governed by the applicable project license, copyright48 * notices, institutional agreements and relevant law.49 *50 * =============================================================================51 * LICENSE & MAINTENANCE52 * =============================================================================53 *54 * License : None specified (no license file in this repository)55 * Classification : Public56 *57 * =============================================================================58 * END OF SP BOUCHER SOURCE HEADER59 * =============================================================================60 */6162// UQO Éval — Métré : plans d'un projet (liste, téléversement multipart : original + pages PNG rendues par le navigateur).63import { NextRequest, NextResponse } from "next/server";64import { HttpError, handle, requireProject, requireUser } from "@/lib/metre/auth";65import { MAX_FILE_BYTES, MAX_PAGES, importDxf, listPlanFiles, storePlanFile } from "@/lib/metre/plans";6667export const runtime = "nodejs";68export const maxDuration = 120;6970export async function GET(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {71 return handle(async () => { const { id } = await params; const ctx = requireUser(req); requireProject(ctx, id, "read"); return NextResponse.json({ plans: listPlanFiles(ctx.db, id) }); });72}7374export async function POST(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {75 return handle(async () => {76 const { id } = await params;77 const ctx = requireUser(req, { limit: { key: "plan.upload", max: 20, windowMs: 10 * 60_000 } });78 requireProject(ctx, id, "write");79 const len = Number(req.headers.get("content-length") ?? 0);80 if (len > MAX_FILE_BYTES + MAX_PAGES * 25 * 1024 * 1024) throw new HttpError(413, "Téléversement trop volumineux.");81 let form: FormData;82 try { form = await req.formData(); } catch { throw new HttpError(400, "Formulaire multipart attendu."); }83 // `instanceof File` n'est pas fiable entre les réalisations (undici / polyfill Next) : on vérifie la forme Blob84 const isBlob = (v: unknown): v is Blob & { name?: string } => !!v && typeof v === "object" && typeof (v as Blob).arrayBuffer === "function";85 const dxf = form.get("dxf");86 if (isBlob(dxf)) {87 const r = await importDxf(ctx, id, { filename: dxf.name || "plan.dxf", text: Buffer.from(await dxf.arrayBuffer()).toString("utf8") });88 return NextResponse.json(r, { status: 201 });89 }90 const original = form.get("original");91 if (!isBlob(original)) throw new HttpError(400, "Champ « original » (fichier) requis.");92 const pages: { pageNo: number; png: Buffer }[] = [];93 for (const [k, v] of form.entries()) {94 const m = k.match(/^page-(\d+)$/);95 if (m && isBlob(v)) pages.push({ pageNo: Number(m[1]), png: Buffer.from(await v.arrayBuffer()) });96 }97 const r = await storePlanFile(ctx, id, { filename: original.name || "plan", original: Buffer.from(await original.arrayBuffer()), pages });98 return NextResponse.json(r, { status: 201 });99 });100}101